package com.think.admin.oauth.handle;

import com.think.admin.oauth.exception.CustomOauth2Exception;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 登录异常
 * </p>
 *
 * @author: xwj
 * @data: 2019-05-07
 */
@Component
public class WebResponseExceptionTranslatorImpl extends DefaultWebResponseExceptionTranslator {
    public static final String BAD_MSG = "坏的凭证";

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
        // 声明的自定义异常
        if (e instanceof OAuth2Exception && e.getMessage().equals(BAD_MSG)) {
            CustomOauth2Exception customOauth2Exception = new CustomOauth2Exception(e.getMessage(),
                    e, ((InvalidGrantException) e).getHttpErrorCode(), "账号或者密码不正确");
            return super.translate(customOauth2Exception);
        } else {
            return super.translate(e);
        }
    }
}
